package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysPermission;
import cn.wgx.modules.sys.services.SysPermissionService;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Api(tags = "3. 系统权限管理", description = "一权限等于一菜单或一功能")
@RestController
@RequestMapping(value="${allPathPre}syspermission")
public class SysPermissionController extends BaseController {

    @Autowired
    private SysPermissionService sysPermissionService;

    @ApiOperation("获取全部权限列表")
    @ApiPermission(value = false)
    @RequestMapping(path = "list", method = RequestMethod.GET)
    public Map<String,Object> getList(
//            @RequestParam(value = "page", defaultValue = "1") Integer page,
//            @RequestParam(value = "pageSize", defaultValue = "9999") Integer pageSize,
            @ApiParam("是否刷新缓存") @RequestParam(defaultValue = "false", required = false)Boolean flush,
            @ApiParam("是否树形数据") @RequestParam(defaultValue = "false", required = false)Boolean tree,
            @ApiParam("只要menu") @RequestParam(defaultValue = "false", required = false)Boolean menu) {
        if(flush){
            sysPermissionService.clearListCache();
        }
//        Page p = PageHelper.startPage(page, pageSize);
        List<SysPermission> list ;

        if(menu){
            list = sysPermissionService.getMenu();
        }else {
            list = sysPermissionService.getList();
        }
        if(tree){
            JSONArray j  = sysPermissionService.sortPermission(list);
            return returnData(j);
        }
        return returnData(list);
    }


    @ApiOperation("新增权限")
    @ApiPermission
    @RequestMapping(path = "", method = RequestMethod.POST)
    public Map<String,Object> insertOne(@RequestBody SysPermission record) {
        return returnData(sysPermissionService.insertOne(record));
    }



    @ApiOperation(value = "更新权限", notes = "Permission_id")
    @ApiPermission
    @RequestMapping(path = "", method = RequestMethod.PATCH)
    public Map<String,Object> updateOne(@RequestBody SysPermission record) {
        return returnData(sysPermissionService.updateOne(record));

    }


    @ApiOperation("删除权限")
    @ApiPermission
    @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
    public Map<String,Object> deleteOne(@PathVariable("id") Integer id) {
        if(1 == id){
            return returnErrorMap("管理员不能删除");
        }
        return returnData(sysPermissionService.deleteOne(id));
    }

    @ApiOperation("清除缓存")
    @ApiPermission(value = false)
    @RequestMapping(path = "flush", method = RequestMethod.GET)
    public Map<String,Object> flushCache() {
        sysPermissionService.clearListCache();
        return returnData("ok");
    }
}